from multiprocessing import Process,Queue
import pygame
import time
ROWS, COLS = 20, 10
grid = [[(r+c) % 2 for c in range(COLS)] for r in range(ROWS)]
CELL = 20
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
FRAME = list[list[int]]
def lcd(frame_queue:Queue):
    pygame.init()
    screen = pygame.display.set_mode((CELL*COLS, CELL*ROWS))
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        if not frame_queue.empty():
            last_frame = frame_queue.get()
            screen.fill(WHITE)
            for r, row in enumerate(last_frame):
                for c, val in enumerate(row):
                    if val == 1:
                        rect = pygame.Rect(c*CELL, r*CELL, CELL, CELL)
                        pygame.draw.rect(screen, BLACK, rect)
            pygame.display.flip()
        clock.tick(60)
    pygame.quit()


def cpu(frame_queue: Queue):
    running = True
    while running:
        for i in range(ROWS):
            grid = [[1 if r == i else 0 for c in range(
                COLS)] for r in range(ROWS)]
            frame_queue.put(grid)
            time.sleep(1.0/60)

if __name__ == "__main__":
    frame_queue= Queue(maxsize=1)
    p_lcd = Process(target=lcd,args=(frame_queue,))
    p_cpu = Process(target=cpu,args=(frame_queue,))
    p_lcd.start()
    p_cpu.start()
    p_lcd.join()
    p_cpu.terminate()